<template>
  <ag-grid-vue
    ref="grid"
    :columnDefs="columnDefs"
    :rowData="rowData"
    :editable="true"
    :style="{ height: gridHeight }"
    :pagination="false"
    :getRowNodeId="function(params){return params.AutoId}"
    class="ag-theme-material"
    @grid-ready="onGridReady"
  ></ag-grid-vue>
</template>
<script>
import NumberEditor from '@/components/CellEditor/NumberEditor.vue'

import { AgGridVue } from 'ag-grid-vue'
export default {
  components: {
    NumberEditor,
    AgGridVue
  },
  props: ['rowData', 'RankEditable'],
  watch: {
    RankEditable: function(val) {
      for (let idx in this.columnDefs) {
        if (this.columnDefs[idx].editable !== undefined) {
          this.columnDefs[idx].editable = val
          this.columnApi.getColumn(
            this.columnDefs[idx].field
          ).colDef.editable = val
        }
      }
    }
  },
  data() {
    return {
      frameworkComponents: null,
      gridHeight: '',
      gridApi: null,
      columnApi: null,
      //rowData: null,
      columnDefs: [
        { headerName: '', field: 'chk', width: 50, checkboxSelection: true },
        {
          headerName: '代号',
          field: 'Code',
          sortable: true,
          filter: true,
          editable: true
        },
        {
          headerName: '名称',
          field: 'Name',
          sortable: true,
          filter: true,
          editable: true
        },
        {
          headerName: '单价',
          field: 'Price',
          sortable: true,
          filter: true,
          cellEditorFramework: 'NumberEditor',
          editable: true
        },
        {
          headerName: '最小值',
          field: 'Min',
          sortable: true,
          filter: true,
          cellEditorFramework: 'NumberEditor',
          editable: this.RankEditable
        },
        {
          headerName: '最大值',
          field: 'Max',
          sortable: true,
          filter: true,
          cellEditorFramework: 'NumberEditor',
          editable: this.RankEditable
        }
      ]
    }
  },
  methods: {
    onGridReady(params) {
      this.gridApi = params.api
      this.columnApi = params.columnApi
    },
    getSelected() {
      const selectedNodes = this.gridApi.getSelectedNodes()
      const selectedData = selectedNodes.map(node => node.data)
      return selectedData
    }
  },
  mounted() {
    this.gridHeight = window.innerHeight - 265 + 'px'
    const that = this
    window.onresize = function changeGridSize() {
      that.gridHeight = window.innerHeight - 265 + 'px'
    }
  }
}
</script>

<style lang="scss">
@import "../../../node_modules/ag-grid-community/dist/styles/ag-grid.css";
@import "../../../node_modules/ag-grid-community/dist/styles/ag-theme-balham.css";
@import "../../../node_modules/ag-grid-community/dist/styles/ag-theme-material.css";
</style>
